﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using EventHandler.Libraries.Business.Database;

namespace EventHandler.Libraries.Business.Security.Authentication
{
    public class DBAuthenticationProvider: IAuthenticationProvider
    {
        private IDatabaseHelper _dbHelper;

        public string Username { get; set; }
        public string Password { get; set; }
        
        public DBAuthenticationProvider(IDatabaseHelper dbHelper,string userName,string password)
        {
            _dbHelper = dbHelper;
            Username = userName;
            Password = password;
        }

        public bool Login()
        {
            bool result = false;

            using (var cn = _dbHelper.GetConnection())
            {
                cn.Open();

                var cmd = cn.CreateCommand();
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "Auth.VerifyAccount";
                var usernameParam = cmd.CreateParameter();
                usernameParam.Size = 255;
                usernameParam.ParameterName = "@Username";
                usernameParam.DbType = DbType.String;
                var passwordParam = cmd.CreateParameter();
                passwordParam.Size = 255;
                passwordParam.ParameterName = "@Password";
                passwordParam.DbType = DbType.String;
                cmd.Parameters.Add(usernameParam);
                cmd.Parameters.Add(passwordParam);

                using(var dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        result = true;
                    }
                }

                cn.Close();
            }
            return result;
        }

    }
}
